
const scpClient = require('scp2')
const Client = require('ssh2').Client;
const service = require('./service');


const ora = require('ora')
const loading = ora(service.name+'正在部署中...，请您去喝口茶稍等片刻。');


let conn = new Client();
conn.on('ready', function() {
  conn.exec(service.command, function(err, stream) {
    if (err) throw err;

    stream.on('close', function() {
      loading.start();
      scpClient.scp('./dist', service, publishCallback);
      conn.end();

    }).on('data', function(data) {
      console.log('STDOUT: ' + data);

    }).stderr.on('data', function(data) {
      console.log('STDERR: ' + data);
    });

  });
}).connect(service);


const publishCallback = function (err){
  loading.stop();
  if (err) {
    console.log(service.name+'部署失败!');
    throw err;
  } else {
    console.log(service.name+'部署成功!');
  }
}

